﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQuarters.WebQuarters.Core;
using OpenQuarters.WebQuarters.Modules.ItemList;
using RssToolkit.Rss;

namespace OpenQuarters.WebQuarters.Modules.MediaGallery
{
    public enum MediaType
    {
        Image,
        Flash
    }

    public partial class MediaItem : MediaItemEntity<MediaItem, int>, IItemListable
    {
        [PropertyConfiguration(PropertyType.Hidden, SourceField = "MediaItemId")]
        [Newtonsoft.Json.JsonProperty]
        public int json_MediaItemId
        {
            get { return this.MediaItemId; }
            set { this.MediaItemId = value; }
        }

        [Filterable, Orderable]
        [PropertyConfiguration(PropertyType.TextBox, SourceField = "Title")]
        [Newtonsoft.Json.JsonProperty]
        public string json_Title
        {
            get { return this.Title; }
            set { this.Title = value; }
        }

        [PropertyConfiguration(PropertyType.TextBox, SourceField = "Summary")]
        [Newtonsoft.Json.JsonProperty]
        public string json_Summary
        {
            get { return this.Summary; }
            set { this.Summary = value; }
        }

        [Filterable, Orderable]
        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox, SourceField = "Category")]
        public string json_Category
        {
            get { return this.Category; }
            set { this.Category = value; }
        }

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.File)]
        public string json_Main_File_Path
        {
            get { return this.FilePath; }
            set { this.FilePath = value; }
        }

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.File)]
        public string json_Alternative_File_Path
        {
            get { return this.AlternativeFilePath; }
            set { this.AlternativeFilePath = value; }
        }

        [Filterable, Orderable]
        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.CheckBox, SourceField = "IsDefault")]
        public bool json_IsDefault
        {
            get { return this.IsDefault; }
            set { this.IsDefault = value; }
        }

        [Filterable, Orderable]
        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.DropDown, SourceField = "MediaType")]
        public MediaType json_MediaType
        {
            get { return this.MediaType; }
            set { this.MediaType = value; }
        }

        [Orderable]
        [PropertyConfiguration(PropertyType.TextBox, SourceField = "Order")]
        [Newtonsoft.Json.JsonProperty]
        public int json_Order
        {
            get { return this.Order; }
            set { this.Order = value; }
        }


        public override int GetKey(MediaItem Entity)
        {
            return Entity.MediaItemId;
        }

        public override System.Linq.Expressions.Expression<Func<MediaItem, bool>> GetIDSelector(int ID)
        {
            return e => e.MediaItemId == ID;
        }

        public string EditLink
        {
            get
            {
                return ItemListModule.GetItemEditLink(this);
            }
        }

        public string DeleteLink
        {
            get
            {
                return ItemListModule.GetItemDeleteLink(this);
            }
        }

        #region IItemListable Members

        public string GetIDAsString()
        {
            return this.MediaItemId.ToString();
        }

        public bool IsNewItem()
        {
            return this.IsNew();
        }

        public IItemListable CreateItem<T>(ItemListerControl<T> parentList) where T : class, IItemListable
        {
            return new MediaItem()
            {
                Title = "Media Item",
                Category = CMSPage.Current.Title,
                FilePath = "/Areas/CMS/Content/Images/image_imagenotfound.gif",
                AlternativeFilePath = "/Areas/CMS/Content/Images/image_imagenotfound.gif",
                //FilePath = "/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Modules.Flash&filename=Resources.flash_flashnotfound.swf",
                //AlternativeFilePath = "/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&filename=Areas.CMS.Content.Images.image_imagenotfound.gif",
                IsDefault = false,
                MediaType = MediaType.Image,
                Order = (Table.Max(mi => (Int32?)mi.Order) ?? 0) 
            };
        }

        public IItemListable LoadItem(string ID)
        {
            return Load(Convert.ToInt32(ID));
        }

        public IItemListable SaveItem()
        {
            return this.Save();
        }

        public void DeleteItem()
        {
            this.Delete();
        }

        public IQueryable GetQueryableList()
        {
            return Table;
        }

        public string ItemUrl()
        {
            throw new NotImplementedException("MediaItems do not have their own Urls");
        }

        public IItemListable GetItemFromUrl(string url, List<IItemListable> additionalItems)
        {
            return null;
        }

        public RssToolkit.Rss.RssItem ToRssItem()
        {
            RssItem item = new RssItem();
            item.Title = this.Title;
            item.PubDateParsed = DateTime.Now;
            item.Link = this.FilePath;

            if (this.Category.Length != 0)
            {
                RssCategory category = new RssCategory();
                category.Text = this.Category;
                item.Categories.Add(category);
            }

            return item;
        }

        public string ItemTitle()
        {
            return this.Title;
        }

        public string MetaKeywords()
        {
            return null;
        }

        public string MetaDescription()
        {
            return null;
        }

        #endregion
    }
}
